/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 09 - 30
 * Time: 10:05
 */
//牛客 把字符串转换为整数
public class Solution11 {
    public int StrToInt(String str) {
        if(str == null || str.isEmpty()) return 0;
        char[] arr = str.toCharArray();
        int flag = 1;
        if(arr[0] == '+') {
            arr[0] = '0';
        }else if(arr[0] == '-'){
            flag = -1;
            arr[0] = '0';
        }
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] < '0' || arr[i] > '9') {
                sum = 0;
                break;
            }
            sum = sum * 10 + arr[i] - '0';
        }
        return flag * sum;
    }
}
